Imports FMPLib01
Imports cmplib01
Imports System.Drawing
Imports System.Drawing.Drawing2D

Public Class cCampaignerMapMaker

    Public Event MapRedrawn()

    Private Font As New Font("arial", 11, FontStyle.Regular, GraphicsUnit.Pixel)
    Private c_SelectionColor As Color = Color.Yellow
    Private m_DeploymentZoneColor As Color = Color.FromArgb(120, 150, 150, 255)

    Private m_Rendered As Boolean
    Private m_BMP As Bitmap   ' a bmp of our map, in its edited mode
    Private m_FMP As New FMPLib01.FlexMapData()
    Private m_Map As Image
    Private m_cmp As CampaignerGameData
    Private m_MapOpened As Boolean = False
    Private _campImages As New CampaignerImageLibrary
    Private m_iconsize As Integer = 24

    Private m_showGrid As DisplayType_dt = DisplayType_dt.NotShown   '// shows grid and scale
    Private m_showLabels As Boolean = True
    Private m_labelColor As Color = Color.Yellow
    Private m_labelSize As Int16 = 16
    Private m_showBorders As Boolean

    Private m_showObjectives As DisplayType_dt = DisplayType_dt.Normal
    Private m_showDeploymentZones As DisplayType_dt = DisplayType_dt.Normal
    Private m_showZOC As DisplayType_dt = DisplayType_dt.Normal
    Private m_showLOS As DisplayType_dt = DisplayType_dt.Normal
    Private m_showWaypoints As DisplayType_dt = DisplayType_dt.Normal
    Private m_showReinforcementRange As DisplayType_dt = DisplayType_dt.Normal
    Private m_showBattles As DisplayType_dt = DisplayType_dt.Normal
    Private m_showAllChits As Boolean = True


    Private _unitSize As IconSize = IconSize.Normal
    Private _battleSize As IconSize = IconSize.Normal

    Private m_selectionColor As Color = c_SelectionColor

    Private _SelectedChit As ChitMember = Nothing
    Private _showMapOnly As Boolean = True



#Region "Map Redraw and Position"
    Public Sub RedrawMap(ByRef fmp As FlexMapData, ByRef cmp As CampaignerGameData)
        m_FMP = fmp
        m_cmp = cmp
        CreateBMP()

    End Sub

    Public Sub RedrawMapForReplay(ByVal phase As Long)

        Dim width As Int16 = m_FMP.Width '* m_ZoomFactor
        Dim height As Int16 = m_FMP.Height '* m_ZoomFactor
        Dim im As Image = New Bitmap(width, height)

        Dim g As Graphics = Graphics.FromImage(im)

        DrawBaseTerrain(g)
        DrawChitsForPhase(g, phase)

        m_BMP = im
        g.Dispose()
        GC.Collect()
        m_Rendered = True
    End Sub

    Public Sub RedrawMapOnly()
        Dim width As Int16 = m_FMP.Width '* m_ZoomFactor
        Dim height As Int16 = m_FMP.Height '* m_ZoomFactor
        Dim im As Image = New Bitmap(width, height)
        Dim g As Graphics = Graphics.FromImage(im)

        DrawBaseTerrain(g)

        m_BMP = im
        g.Dispose()
        GC.Collect()
        m_Rendered = True
    End Sub

    Overridable Sub CreateBMP()

        If m_cmp Is Nothing Then Exit Sub

        If Not m_FMP Is Nothing Then
            'If m_Rendered = False Then Exit Sub

            Dim width As Int16 = m_FMP.Width '* m_ZoomFactor
            Dim height As Int16 = m_FMP.Height '* m_ZoomFactor
            Dim im As Image = New Bitmap(width, height)
            Dim g As Graphics = Graphics.FromImage(im)

            DrawBaseTerrain(g)

            '// everything gets drawn. Yah.
            DrawGrid(g)
            DrawLabels(g)

            Try
                If m_cmp.selectedPlayer.PlayerName = "<Moderator>" Then

                    'DrawChitsForModerator(g)

                    m_BMP = im
                    g.Dispose()
                    GC.Collect()
                    m_Rendered = True
                    Exit Sub
                End If

                If _showMapOnly = False Then
                    DrawObjectives(g)
                    DrawDeploymentZones(g)

                    If m_showAllChits Then
                        '// none of these are drawn if we aren't drawing chits! :D (though ZoC would be cool to show)
                        DrawOptions(g)
                        DrawChits(g)
                        DrawEnemyDetected(g)
                    End If

                    DrawBattles(g)
                End If
            Catch ex As Exception
                HandleException("cCampaignerMapMaker.CreateBMP", ex)
            End Try

            m_BMP = im

            g.Dispose()
            GC.Collect()

            m_Rendered = True

        End If
    End Sub

    Private Sub DrawBaseTerrain(ByRef g As Graphics)
        g.Clear(Color.Blue)

        Dim tickStart As Date
        Dim tickstop As Date

        Dim RenderTickCount As Int64

        ' if this is the first time rendering this map, then procede as normal. If we are redrawing the map
        ' then we should set the basebmp

        tickStart = Now
        ' if we are editable, then each render is a hardrender

        If m_MapOpened = True Then
            m_FMP.Render(g, g.VisibleClipBounds, False, , 1, 1, , , , False)
        Else
            m_FMP.Render(g, g.VisibleClipBounds, False, , 1, 1)
        End If

        DrawRoads(g)
        DrawBridges(g)

        tickstop = Now
        RenderTickCount = tickstop.Ticks - tickStart.Ticks

    End Sub

#End Region


#Region "Supporting Map: Labels, Bridges"
    Private Sub DrawRoads(ByRef g As Graphics)
        Dim aroad As FMPLib01.Road
        Dim sp As New Pen(Color.Brown, 2)
        Dim spborder As New Pen(Color.Black, 4)

        If m_FMP.Roads Is Nothing Then m_FMP.Roads = New FMPLib01.Roads

        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias
        For Each aroad In m_FMP.Roads
            If aroad.RoadType = FMPLib01.FlexMapData.RoadType.MajorRoad Then
                sp.Width = 1
                spborder.Width = 5
                sp.Color = Color.Yellow
                spborder.Color = Color.Black
            Else
                sp.Width = 2
                spborder.Width = 4
                sp.Color = Color.Brown
                spborder.Color = Color.Black
            End If
            g.DrawLine(spborder, aroad.StartPoint, aroad.EndPoint)
            g.DrawLine(sp, aroad.StartPoint, aroad.EndPoint)
        Next
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed
        sp.Dispose()
        spborder.Dispose()

    End Sub

    Private Sub DrawLabels(ByVal g As Graphics)
        If m_showLabels = False Then Exit Sub

        Dim lab As FMPLib01.FlexMapData.MapLabel
        Dim br As New SolidBrush(m_labelColor)

        Try
            For Each lab In FMPData.Labels
                Dim pth As New GraphicsPath
                Dim pnt As New PointF(lab.x, lab.y)

                pth.AddString(lab.text, System.Drawing.FontFamily.GenericSerif, System.Drawing.FontStyle.Regular, m_labelSize, pnt, System.Drawing.StringFormat.GenericDefault)

                Dim bounds As RectangleF = pth.GetBounds
                Dim boundsBrush As New SolidBrush(lab.backgroundColor)
                ' increase the size of the box by 1 pixel all the way around
                bounds.X -= 1
                bounds.Y -= 1
                bounds.Width += 3
                bounds.Height += 3

                ' do the markbox. The markbox is a square around the actual position of the label, its basically an arrow.
                Dim markBoxSolid As New RectangleF(bounds.X - 3, bounds.Y - 3, 6, 6)
                Dim markBrush As New SolidBrush(Color.Red)
                Dim markBoxBounds As New Rectangle(markBoxSolid.X, markBoxSolid.Y, markBoxSolid.Width, markBoxSolid.Height)
                g.FillRectangle(markBrush, markBoxSolid)
                g.DrawRectangle(Pens.Black, markBoxBounds)
                markBrush.Dispose()

                Dim boundsBox As New Rectangle(bounds.X, bounds.Y, bounds.Width, bounds.Height)
                g.FillRectangle(boundsBrush, bounds)
                g.DrawRectangle(Pens.Black, boundsBox)
                boundsBrush.Dispose()

                Dim textBrush As New SolidBrush(lab.foreColor)
                'br.Color = lab.foreColor
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias
                g.FillPath(textBrush, pth)
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.Default
                textBrush.Dispose()
                DrawTemporaryLabels(g)
            Next

        Catch ex As Exception
            'HandleException(c_Module, ex)
        End Try

    End Sub

    Private Sub DrawTemporaryLabels(ByVal g As Graphics)
        ' this is used so we can totally draw the really cool labels that are in a CGD file and not stored in the map. Its cool.
        ' trust me.
        Dim lab As FMPLib01.FlexMapData.MapLabel
        'Dim fnt As New Font("arial", m_labelSize)
        Dim br As New SolidBrush(m_labelColor)

        Try
            For Each lab In FMPData.TemporaryLabels
                Dim pth As New GraphicsPath
                Dim pnt As New PointF(lab.x, lab.y)

                pth.AddString(lab.text, System.Drawing.FontFamily.GenericSerif, System.Drawing.FontStyle.Regular, m_labelSize, pnt, System.Drawing.StringFormat.GenericDefault)

                Dim bounds As RectangleF = pth.GetBounds
                Dim boundsBrush As New SolidBrush(lab.backgroundColor)
                ' increase the size of the box by 1 pixel all the way around
                bounds.X -= 1
                bounds.Y -= 1
                bounds.Width += 3
                bounds.Height += 3

                ' do the markbox. The markbox is a square around the actual position of the label, its basically an arrow.
                Dim markBoxSolid As New RectangleF(bounds.X - 3, bounds.Y - 3, 6, 6)
                Dim markBrush As New SolidBrush(Color.Red)
                Dim markBoxBounds As New Rectangle(markBoxSolid.X, markBoxSolid.Y, markBoxSolid.Width, markBoxSolid.Height)
                g.FillRectangle(markBrush, markBoxSolid)
                g.DrawRectangle(Pens.Black, markBoxBounds)
                markBrush.Dispose()

                Dim boundsBox As New Rectangle(bounds.X, bounds.Y, bounds.Width, bounds.Height)
                g.FillRectangle(boundsBrush, bounds)
                g.DrawRectangle(Pens.Black, boundsBox)
                boundsBrush.Dispose()

                Dim textBrush As New SolidBrush(lab.foreColor)
                'br.Color = lab.foreColor
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias
                g.FillPath(textBrush, pth)
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.Default
                textBrush.Dispose()

            Next
        Catch ex As Exception
            '    HandleException(c_Module, ex)
        End Try
    End Sub

    Private Sub DrawBridges(ByVal g As Graphics)
        Dim bri As FMPLib01.FlexMapData.Bridge

        Try
            For Each bri In FMPData.Bridges
                Dim bmp As Bitmap
                Dim bridgeCoord As PointF = New PointF(bri.x, bri.y)

                Select Case bri.type
                    Case FMPLib01.FlexMapData.BridgeType.temporaryBridge
                        bmp = _campImages.temporaryBridge
                    Case FMPLib01.FlexMapData.BridgeType.smallBridge
                        bmp = _campImages.smallBridge
                    Case FMPLib01.FlexMapData.BridgeType.mediumBridge
                        bmp = _campImages.mediumBridge
                    Case FMPLib01.FlexMapData.BridgeType.heavyBridge
                        bmp = _campImages.heavyBridge
                End Select


                bmp.MakeTransparent(Color.FromArgb(255, 0, 255))
                bridgeCoord.X -= bmp.Width / 2
                bridgeCoord.Y -= bmp.Height / 2

                ' draw the bridge
                g.DrawImage(bmp, bridgeCoord.X, bridgeCoord.Y)
            Next
        Catch ex As Exception
            'HandleException(c_Module, ex)
            MessageBox.Show(ex.Message)
        End Try

    End Sub

    Public Sub DrawScale(ByRef g As Graphics)

        Try
            Dim scaleLineStart As New Point(40, 40)
            Dim scaleLineEnd As New Point(FMPData.MapScale.PixelCount + 40, 40)
            Dim scaleLinePen As New Pen(Color.Yellow, 4)
            Dim rect As New RectangleF(30, 30, (scaleLineEnd.X - 20), 30)

            If rect.Width < 60 Then rect.Width = 60
            Dim rectBrush As New SolidBrush(Color.FromArgb(100, 0, 0, 0))
            Dim miles As Integer = FMPData.MapScale.MilesPerPixelCount
            Dim milesText As String
            If miles < 2 Then
                milesText = " mile"
            Else
                milesText = " miles"
            End If
            g.FillRectangle(rectBrush, rect)
            g.DrawRectangle(Pens.Black, New Rectangle(rect.X, rect.Y, rect.Width, rect.Height))
            g.DrawLine(scaleLinePen, scaleLineStart, scaleLineEnd)
            g.DrawString(miles & milesText, Me.Font, Brushes.Yellow, 45, 45)

        Catch ex As Exception
            '    HandleException(c_Module, ex)
        End Try
    End Sub

    Private Sub DrawGrid(ByRef g As Graphics)
        If m_showGrid = DisplayType_dt.NotShown OrElse m_showGrid = DisplayType_dt.Reduced Then Exit Sub

        Try
            Dim x As Integer
            Dim y As Integer
            Dim stepCounter As Integer = 0 ' just before the first letter character in the ascii codes
            Dim f As New Font("arial", 12, FontStyle.Bold)

            Dim fatterpen As New Pen(Color.FromArgb(100, 210, 210, 210), 3)

            For x = 1 To FMPData.Width Step FMPData.MapScale.PixelCount
                ' draw the vertical lines
                g.DrawLine(fatterpen, x, 0, x, FMPData.Height)
                g.DrawLine(Pens.DarkBlue, x, 0, x, FMPData.Height)
                ' draw a grid identifier
                g.DrawString(stepCounter, f, Brushes.DarkBlue, x - (FMPData.MapScale.PixelCount / 2), 10)
                stepCounter += 1
            Next

            stepCounter = 0

            For y = 1 To FMPData.Height Step FMPData.MapScale.PixelCount
                ' draw a line for the grid
                g.DrawLine(fatterpen, 0, y, FMPData.Width, y)
                g.DrawLine(Pens.DarkBlue, 0, y, FMPData.Width, y)
                ' draw grid identifier
                g.DrawString(stepCounter, f, Brushes.DarkBlue, 10, y - (FMPData.MapScale.PixelCount / 2))
                stepCounter += 1
            Next

        Catch ex As Exception
            '    HandleException(c_Module, ex)
        End Try

    End Sub

    Private Sub DrawObjectives(ByRef g As Graphics)

        If m_cmp Is Nothing Then Exit Sub
        If m_showObjectives = DisplayType_dt.NotShown Then Exit Sub


        Dim AO As AreaObjective

        Dim OwnedAO As New HatchBrush(HatchStyle.Cross, Color.FromArgb(30, 220, 220, 220), Color.FromArgb(120, 20, 120, 20))
        Dim UncontrolledAO As New HatchBrush(HatchStyle.WideUpwardDiagonal, Color.FromArgb(30, 220, 220, 220), Color.FromArgb(120, 20, 20, 20))
        Dim EnemyAO As New HatchBrush(HatchStyle.Cross, Color.FromArgb(30, 120, 20, 20), Color.FromArgb(120, 120, 20, 20))
        Dim ContestedAO As New HatchBrush(HatchStyle.WideDownwardDiagonal, Color.FromArgb(100, 220, 20, 20), Color.FromArgb(100, 20, 220, 20))

        Try
            For Each AO In m_cmp.AreaObjectives
                Dim zonePath As New System.Drawing.Drawing2D.GraphicsPath
                Dim pnt As PointF
                Dim lastPnt As PointF
                Dim i As Int16

                For Each pnt In AO.BoundaryPath.Points
                    If i <> 0 Then
                        zonePath.AddLine(lastPnt, pnt)
                        lastPnt = pnt
                    Else
                        lastPnt = pnt
                    End If
                    i += 1
                Next
                i = 0
                ' determine who's pov we are in and create the brush
                Dim AOBrush As Drawing2D.HatchBrush

                If AO.OwningFaction = -1 Then
                    AOBrush = UncontrolledAO
                ElseIf AO.OwningFaction = m_cmp.selectedFaction.FactionID Then
                    AOBrush = OwnedAO
                Else
                    AOBrush = EnemyAO
                End If
                ' down here we catch finally if its contested
                If AO.IsContested Then
                    AOBrush = ContestedAO
                End If

                g.FillPath(AOBrush, zonePath)

                ' if this one is selected, yellow the border
                If AO.IsSelected Then
                    g.DrawPath(Pens.Yellow, zonePath)
                Else
                    g.DrawPath(Pens.Black, zonePath)
                End If

                ' draw the name
                If m_showObjectives = DisplayType_dt.Normal Then
                    Dim boundRect As RectangleF
                    boundRect = AO.ObjectiveRegion.GetBounds(g)
                    ' TODO: Smarter deploymentzone name viewing
                    g.DrawString(AO.BoundaryName, Me.Font, Brushes.Black, boundRect.X + (boundRect.Width / 4), boundRect.Y + (boundRect.Height / 4))
                End If

            Next
        Catch ex As Exception
            '            HandleException(c_Module, ex)
        End Try



    End Sub

    Private Sub DrawDeploymentZones(ByRef g As Graphics)
        Try

            If m_cmp Is Nothing Then Exit Sub
            If m_showDeploymentZones = DisplayType_dt.NotShown Then Exit Sub

            Dim deploymentBrush As New SolidBrush(m_DeploymentZoneColor)
            Dim key As String
            Dim dep As New cmplib01.DeploymentZone

            For Each key In m_cmp.DeploymentZones_g.HT.Keys
                dep = m_cmp.DeploymentZones_g.Item(key)
                Dim zonePath As New System.Drawing.Drawing2D.GraphicsPath
                Dim pnt As PointF
                Dim lastPnt As PointF
                Dim i As Int16
                Dim reg As New Region
                Dim boundRect As RectangleF
                ' if there are points available for deployment, go ahead and draw the zone. If not, don't draw it.
                If dep.MaxDeploymentPerTurn - dep.PointsDeployedThisTurn > 0 Then
                    For Each pnt In dep.BoundaryPath.Points
                        If i <> 0 Then
                            zonePath.AddLine(lastPnt, pnt)
                            lastPnt = pnt
                        Else
                            lastPnt = pnt
                        End If
                        i += 1
                    Next
                    i = 0

                    If m_cmp.Faction_g.Item(m_cmp.selectedPlayer.FactionKey).FactionID = dep.FactionID Then
                        deploymentBrush.Color = m_DeploymentZoneColor
                        g.FillPath(deploymentBrush, zonePath)
                        zonePath.CloseAllFigures()
                        ' now, we get a bounding rectangle and write the deployment zone name in the center
                        reg.Intersect(zonePath)
                        boundRect = reg.GetBounds(g)
                        ' TODO: Smarter deploymentzone name viewing
                        g.DrawString(dep.ZoneName, Me.Font, Brushes.Black, boundRect.X + (boundRect.Width / 4), boundRect.Y + (boundRect.Height / 4))
                        g.DrawString(dep.MaxTotalDeploy.ToString & " total", Me.Font, Brushes.Black, boundRect.X + (boundRect.Width / 4), boundRect.Y + (boundRect.Height / 4) + 12)
                        g.DrawString("(" & dep.MaxDeploymentPerTurn - dep.PointsDeployedThisTurn & " this turn)", Me.Font, Brushes.Black, boundRect.X + (boundRect.Width / 4), boundRect.Y + (boundRect.Height / 4) + 24)
                    End If
                End If
            Next
            deploymentBrush.Dispose()
        Catch argEx As ArgumentOutOfRangeException
            ' whew. 
        Catch ex As Exception
            'HandleException(c_Module, ex)
        End Try
    End Sub

    Private Sub DrawOptions(ByRef g As Graphics)
        ' make sure we have the correct faction
        If m_cmp Is Nothing Then Exit Sub
        If m_cmp.selectedPlayer Is Nothing Then Exit Sub

        Dim pixelsPerOneMile As Integer = m_FMP.MapScale.PixelCount / m_FMP.MapScale.MilesPerPixelCount
        Dim selectedPlayerFaction As Faction = m_cmp.Faction_g.Item(m_cmp.selectedPlayer.FactionKey)

        For Each mem As ChitMember In m_cmp.Chits
            Dim pnt As Point
            Dim optionPoint As Point

            pnt = mem.Location
            pnt.X -= (m_iconsize / 2) '/ MyBase.ZoomFactor
            pnt.Y -= (m_iconsize / 2) '/ MyBase.ZoomFactor
            optionPoint.X = mem.Location.X
            optionPoint.Y = mem.Location.Y

            If mem.OwningFaction.Equals(SelectedPlayerFaction) Then
                Select Case mem.Status
                    Case GroundUnitStatus_cgd.Assembling, GroundUnitStatus_cgd.Engaged, GroundUnitStatus_cgd.Moving, GroundUnitStatus_cgd.OK, GroundUnitStatus_cgd.Regrouping, GroundUnitStatus_cgd.Reinforcing
                        ' ZoC
                        If m_showZOC = DisplayType_dt.Normal Then
                            DrawZOC(g, optionPoint, mem.ZOC * pixelsPerOneMile, , mem.Status)
                        End If

                        ' chit position
                        If mem.UnitType = GroundChitClass_cgd.Battlegroup Then
                            DrawFilledRectangle(g, pnt.X - 2, pnt.Y - 2, CInt((m_iconsize) + 4), CInt((m_iconsize) + 4))
                        End If

                        ' los
                        If m_showLOS = DisplayType_dt.Normal Then
                            DrawBorderedCircle(g, optionPoint, (mem.LOS) * pixelsPerOneMile, Color.Black, Color.White)
                        End If

                        '// non-installation options
                        If mem.UnitType <> GroundChitClass_cgd.Objective Then
                            ' reinforcement range
                            If m_showReinforcementRange = DisplayType_dt.Normal Then
                                drawReinforcementRange(g, optionPoint, (DirectCast(mem, BattlegroupChit).effectiveReinforcement * pixelsPerOneMile))
                            End If

                            ' waypoints
                            If m_showWaypoints = DisplayType_dt.Normal Then
                                DrawWaypoints(g, mem)
                            End If

                        End If

                End Select

            End If

        Next
    End Sub

    Private Sub DrawChits(ByRef g As Graphics)
        Try
            Dim enemyBorderPen As New Pen(Color.Red, 3)
            Dim selectedBorderPen As New Pen(Color.Gold, 3)
            Dim drawPoint As Point

            For Each chitmem As ChitMember In m_cmp.Chits
                drawPoint = New Point(chitmem.Location.X - (m_iconsize / 2), chitmem.Location.Y - (m_iconsize / 2))

                If chitmem.OwningFaction.Equals(m_cmp.selectedFaction) Then
                    '// same faction
                    Select Case chitmem.Status
                        Case GroundUnitStatus_cgd.OK, GroundUnitStatus_cgd.Regrouped, GroundUnitStatus_cgd.Assembling, GroundUnitStatus_cgd.Engaged, GroundUnitStatus_cgd.Moving
                            chitmem.icon.MakeTransparent(Color.FromArgb(255, 0, 255))
                            If chitmem.UnitType = GroundChitClass_cgd.Objective Then
                                g.DrawImage(DirectCast(chitmem, InstallationChit).ptrInstallation.Icon(0), drawPoint.X, drawPoint.Y, m_iconsize, m_iconsize)
                                '// we also check to see if this installation has a rule that is inactive.
                                For Each ru As Rule In chitmem.chitRules
                                    If ru.Active = False Then
                                        If ru.ptrRule.AppliesDuring = RuleApplies_cgd.Constant Then g.DrawImage(_campImages.inactiveRule, drawPoint)
                                        '// we only need one to display the icon.
                                        Exit For
                                    End If
                                Next
                            Else
                                g.DrawImage(chitmem.icon, drawPoint.X, drawPoint.Y, m_iconsize, m_iconsize)
                            End If

                        Case GroundUnitStatus_cgd.Dead
                            g.DrawImage(CampaignerImages.deadFriendly, drawPoint.X, drawPoint.Y, m_iconsize, m_iconsize)
                    End Select

                    '// if it's selected
                    If chitmem.Equals(SelectedChit) Then
                        g.DrawRectangle(selectedBorderPen, drawPoint.X - 1, drawPoint.Y - 1, m_iconsize, m_iconsize)
                    End If

                Else
                    'TASK: remove this 
                    '// border
                    'g.DrawRectangle(enemyBorderPen, drawPoint.X - 1, drawPoint.Y - 1, m_iconsize, m_iconsize)
                End If
            Next
        Catch ex As Exception

        End Try
    End Sub

    Private Sub DrawChitsForPhase(ByRef g As Graphics, ByVal phase As Long)
        Try
            For Each mem As ChitMember In m_cmp.Chits

                If Not mem.OwningFaction.Equals(m_cmp.selectedFaction) Then
                    '// more catching the old stuff
                    If mem.detectionStatus Is Nothing Then mem.detectionStatus = New Hashtable

                    Dim deteve As HistoricalDetectionStatusEntry = mem.detectionStatus.Item(phase)
                    '// see if there is an entry for detection for this phase
                    If Not deteve Is Nothing Then
                        If mem.UnitType = GroundChitClass_cgd.Objective Then
                            Select Case deteve.detectionStatus
                                Case ChitDetectionLevel_cdl.Dead
                                    g.DrawImage(Me.CampaignerImages.deadEnemy, mem.Location.X, mem.Location.Y, m_iconsize, m_iconsize)
                                Case ChitDetectionLevel_cdl.Undetected
                                Case ChitDetectionLevel_cdl.Detected
                                    '// this is the almighty question mark
                                    g.DrawImage(Me.CampaignerImages.spottedEnemy, CInt(mem.Location.X - (m_iconsize / 2)), CInt(mem.Location.Y - (m_iconsize / 2)), m_iconsize, m_iconsize)
                                Case Else
                                    g.DrawImage(DirectCast(mem, InstallationChit).ptrInstallation.Icon(0), CInt(mem.Location.X - (m_iconsize / 2)), CInt(mem.Location.Y - (m_iconsize / 2)), m_iconsize, m_iconsize)
                            End Select
                        ElseIf mem.UnitType = GroundChitClass_cgd.Battlegroup Then
                            Dim bg As BattlegroupChit = mem

                            Select Case deteve.detectionStatus
                                Case ChitDetectionLevel_cdl.Dead
                                    g.DrawImage(CampaignerImages.deadEnemy, bg.HistoricalPosition2(phase).x, bg.HistoricalPosition2(phase).y, m_iconsize, m_iconsize)
                                Case ChitDetectionLevel_cdl.Undetected
                                    '// guess who we don't draw
                                Case ChitDetectionLevel_cdl.Detected
                                    g.DrawImage(Me.CampaignerImages.spottedEnemy, bg.HistoricalPosition2(phase).x, bg.HistoricalPosition2(phase).y, m_iconsize, m_iconsize)
                                Case Else
                                    g.DrawImage(bg.icon, bg.HistoricalPosition2(phase).x, bg.HistoricalPosition2(phase).y, m_iconsize, m_iconsize)
                            End Select
                        End If
                    End If
                Else
                    '// same faction, draw historical location
                    Select Case mem.Status
                        Case GroundUnitStatus_cgd.OK, GroundUnitStatus_cgd.Regrouped, GroundUnitStatus_cgd.Assembling, GroundUnitStatus_cgd.Engaged, GroundUnitStatus_cgd.Moving
                            mem.icon.MakeTransparent(Color.FromArgb(255, 0, 255))
                            If mem.UnitType = GroundChitClass_cgd.Objective Then
                                g.DrawImage(DirectCast(mem, InstallationChit).ptrInstallation.Icon(0), CInt(mem.Location.X - (m_iconsize / 2)), CInt(mem.Location.Y - (m_iconsize / 2)), m_iconsize, m_iconsize)
                            Else
                                g.DrawImage(mem.icon, DirectCast(mem, BattlegroupChit).HistoricalPosition2(phase).x, DirectCast(mem, BattlegroupChit).HistoricalPosition2(phase).y, m_iconsize, m_iconsize)
                            End If

                        Case GroundUnitStatus_cgd.Dead
                            g.DrawImage(CampaignerImages.deadFriendly, DirectCast(mem, BattlegroupChit).HistoricalPosition2(phase).x, DirectCast(mem, BattlegroupChit).HistoricalPosition2(phase).y, m_iconsize, m_iconsize)
                    End Select
                End If
            Next
        Catch ex As Exception

        End Try
    End Sub

    Private Sub DrawChitsForModerator(ByRef g As Graphics)
        Try
            Dim Faction1Pen As New Pen(Color.Green, 3)
            Dim Faction2Pen As New Pen(Color.Brown, 3)
            Dim drawPoint As Point

            Dim faction1 As Faction

            For Each key As String In m_cmp.Faction_g.HT.Keys
                faction1 = m_cmp.Faction_g.Item(key)
                Exit For
            Next

            '// now, draw chitties.
            For Each chitmem As ChitMember In m_cmp.Chits
                Select Case chitmem.Status
                    Case GroundUnitStatus_cgd.OK, GroundUnitStatus_cgd.Regrouped, GroundUnitStatus_cgd.Assembling, GroundUnitStatus_cgd.Engaged, GroundUnitStatus_cgd.Moving
                        chitmem.icon.MakeTransparent(Color.FromArgb(255, 0, 255))
                        If chitmem.UnitType = GroundChitClass_cgd.Objective Then
                            g.DrawImage(DirectCast(chitmem, InstallationChit).ptrInstallation.Icon(0), drawPoint.X, drawPoint.Y, m_iconsize, m_iconsize)
                        Else
                            g.DrawImage(chitmem.icon, drawPoint.X, drawPoint.Y, m_iconsize, m_iconsize)
                        End If

                    Case GroundUnitStatus_cgd.Dead
                        If chitmem.OwningFaction.Equals(faction1) Then
                            g.DrawImage(CampaignerImages.deadFriendly, drawPoint.X, drawPoint.Y, m_iconsize, m_iconsize)
                        Else
                            g.DrawImage(CampaignerImages.deadEnemy, drawPoint.X, drawPoint.Y, m_iconsize, m_iconsize)
                        End If
                End Select
                '// now draw the correct color border
                If chitmem.OwningFaction.Equals(faction1) Then
                    g.DrawRectangle(Faction1Pen, drawPoint.X - 1, drawPoint.Y - 1, m_iconsize, m_iconsize)
                Else
                    g.DrawRectangle(Faction2Pen, drawPoint.X - 1, drawPoint.Y - 1, m_iconsize, m_iconsize)
                End If
            Next

        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try
    End Sub

    Private Sub DrawEnemyDetected(ByRef g As Graphics)
        If m_cmp Is Nothing Then Exit Sub

        Dim det As ChitDetection
        Dim target As ChitMember
        Dim enemyBorderPen As New Pen(Color.Red, 3)

        For Each key As String In m_cmp.selectedFaction.HTChitDetection.Keys
            det = m_cmp.selectedFaction.HTChitDetection.Item(key)
            target = det.Chit
            Dim enemyLocation As Point = target.Location
            '// this is for the replay

            enemyLocation = target.Location
            enemyLocation.X -= (m_iconsize / 2)
            enemyLocation.Y -= (m_iconsize / 2)

            ' if its dead, draw it that way
            If target.Status = GroundUnitStatus_cgd.Dead Then
                Dim bmp As Bitmap = CampaignerImages.deadEnemy
                bmp.MakeTransparent(Color.Magenta)
                g.DrawImage(bmp, enemyLocation.X, enemyLocation.Y, m_iconsize, m_iconsize)
            Else
                Select Case det.DetectionLevel
                    Case Is <= 1
                        g.DrawImage(Me.CampaignerImages.spottedEnemy, enemyLocation.X, enemyLocation.Y, m_iconsize, m_iconsize)
                    Case Is <= 2
                        g.DrawImage(Me.CampaignerImages.spottedEnemy, enemyLocation.X, enemyLocation.Y, m_iconsize, m_iconsize)
                    Case Else
                        '// check for missing icon
                        If target.icon Is Nothing Then
                            g.DrawImage(Me.CampaignerImages.unknownIcon, enemyLocation.X, enemyLocation.Y, m_iconsize, m_iconsize)
                        Else
                            If target.UnitType = GroundChitClass_cgd.Objective Then
                                g.DrawImage(DirectCast(target, InstallationChit).ptrInstallation.Icon(0), enemyLocation.X, enemyLocation.Y, m_iconsize, m_iconsize)
                            Else
                                g.DrawImage(target.icon, enemyLocation.X, enemyLocation.Y, m_iconsize, m_iconsize)
                            End If

                            '// now the proper border. RED RED RED.
                            g.DrawRectangle(enemyBorderPen, enemyLocation.X, enemyLocation.Y, m_iconsize, m_iconsize)

                        End If

                End Select
            End If

        Next
    End Sub

    Private Sub DrawChitsForReplay(ByRef g As Graphics, ByVal phase As Integer)
        Try

        Catch ex As Exception

        End Try
    End Sub

    Private Sub DrawEnemyForReplay(ByRef g As Graphics, ByVal phase As Integer)

    End Sub

    Private Sub DrawBattles(ByRef g As Graphics)
        Try

        Catch ex As Exception

        End Try
    End Sub


    Private Sub DrawWaypoints(ByRef g As Graphics, ByVal mem As ChitMember)
        If mem.UnitType = GroundChitClass_cgd.Objective Then Exit Sub
        If m_cmp.selectedPlayer Is Nothing Then Exit Sub

        'If GC.GroundChitClass = GroundChitClass_cgd.Objective Then Exit Sub
        'If ptrCurrentGameData.selectedPlayer Is Nothing Then Exit Sub
        'If m_ReplayMode Then Exit Sub

        Dim batt As BattlegroupChit = DirectCast(mem, BattlegroupChit)

        If batt.waypoints.Count > 0 Then
            Dim waypoint As IWaypointLine
            Dim wap As New WaypointLine
            Dim selUnitWap As New SelectedUnitWaypointLine
            Dim friendlyWap As New FriendlyPlayerWaypoint
            Dim invalidWap As New InvalidWaypoint
            Dim waypointCount As Int16 = batt.waypoints.Count

            ' the selected chit is always drawn with a yellow waypoint line.
            If Not _SelectedChit Is Nothing Then
                If batt Is _SelectedChit Then
                    If batt.invalidWaypoint Then
                        waypoint = invalidWap
                    Else
                        waypoint = selUnitWap
                    End If

                Else
                    If batt.OwningPlayer.Equals(m_cmp.selectedPlayer) Then
                        If batt.invalidWaypoint = True Then
                            waypoint = invalidWap
                        Else
                            waypoint = wap
                        End If
                    Else
                        waypoint = friendlyWap
                    End If
                End If
            Else
                If batt.OwningPlayer.Equals(m_cmp.selectedPlayer) Then
                    If batt.invalidWaypoint = True Then
                        waypoint = invalidWap
                    Else
                        waypoint = wap
                    End If
                Else
                    waypoint = friendlyWap
                End If
            End If

            ' draw the first waypoint, drawn from the unit to the first point
            waypoint.pnts.Add(batt.Location)
            waypoint.pnts.Add(batt.waypoints.item(0).LOC)

            ' draw each successive waypoint
            If waypointCount > 1 Then
                For waypointCount = 0 To waypointCount - 1
                    waypoint.pnts.Add(batt.waypoints.item(waypointCount).LOC)
                Next
            End If
            waypoint.Draw(g)
        End If

    End Sub

#End Region


#Region "Properties"

    Public Property ShowMapOnly() As Boolean
        Get
            Return _showMapOnly
        End Get
        Set(ByVal value As Boolean)
            _showMapOnly = value
        End Set
    End Property

    Public Property ShowBorders() As Boolean
        Get
            Return m_showBorders
        End Get
        Set(ByVal value As Boolean)
            m_showBorders = value
        End Set
    End Property

    Public Property SelectedChit() As ChitMember
        Get
            Return _SelectedChit
        End Get
        Set(ByVal value As ChitMember)
            _SelectedChit = value
        End Set
    End Property

    Public Property ShowAllChits() As Boolean
        Get
            Return m_showAllChits
        End Get
        Set(ByVal value As Boolean)
            m_showAllChits = value
        End Set
    End Property

    Public Property showBattles() As DisplayType_dt
        Get
            Return m_showBattles
        End Get
        Set(ByVal value As DisplayType_dt)
            m_showBattles = value
        End Set
    End Property

    Public Property showReinforcementRange() As DisplayType_dt
        Get
            Return m_showReinforcementRange
        End Get
        Set(ByVal value As DisplayType_dt)
            m_showReinforcementRange = value
        End Set
    End Property

    Public Property showWaypoints() As DisplayType_dt
        Get
            Return m_showWaypoints
        End Get
        Set(ByVal value As DisplayType_dt)
            m_showWaypoints = value
        End Set
    End Property

    Public Property showZOC() As DisplayType_dt
        Get
            Return m_showZOC
        End Get
        Set(ByVal value As DisplayType_dt)
            m_showZOC = value
        End Set
    End Property

    Public Property showLOS() As DisplayType_dt
        Get
            Return m_showLOS
        End Get
        Set(ByVal value As DisplayType_dt)
            m_showLOS = value
        End Set
    End Property

    Public Property showObjectives() As DisplayType_dt
        Get
            Return m_showObjectives
        End Get
        Set(ByVal value As DisplayType_dt)
            m_showObjectives = value
        End Set
    End Property

    Public Property showDeploymentZones() As DisplayType_dt
        Get
            Return m_showDeploymentZones
        End Get
        Set(ByVal value As DisplayType_dt)
            m_showDeploymentZones = value
        End Set
    End Property

    Public Property ShowGrid() As DisplayType_dt
        Get
            Return m_showGrid
        End Get
        Set(ByVal value As DisplayType_dt)
            m_showGrid = value
        End Set
    End Property

    Public Property CampaignerImages() As CampaignerImageLibrary
        Get
            Return _campImages
        End Get
        Set(ByVal value As CampaignerImageLibrary)
            _campImages = value
        End Set
    End Property

    Public Property LabelSize() As Int16
        Get
            Return m_labelSize
        End Get
        Set(ByVal Value As Int16)
            m_labelSize = Value
        End Set
    End Property

    Public Property LabelColor() As Color
        Get
            Return m_labelColor
        End Get
        Set(ByVal Value As Color)
            m_labelColor = Value
        End Set
    End Property

    Public Property ShowLabels() As Boolean
        Get
            Return m_showLabels
        End Get
        Set(ByVal Value As Boolean)
            m_showLabels = Value
        End Set
    End Property

    Protected ReadOnly Property FMP() As FMPLib01.FlexMapData
        Get
            Return m_FMP
        End Get

    End Property

    Protected Property Rendered_b() As Boolean
        Get
            Return m_Rendered
        End Get
        Set(ByVal Value As Boolean)
            m_Rendered = Value
        End Set
    End Property

    Protected Property BMP() As Bitmap
        Get
            Return m_BMP
        End Get
        Set(ByVal Value As Bitmap)
            m_BMP = Value
        End Set
    End Property

    Public Property FMPData() As FMPLib01.FlexMapData
        Get
            Return m_FMP
        End Get
        Set(ByVal Value As FMPLib01.FlexMapData)
            If Value Is Nothing Then Exit Property

            m_FMP = Value
            m_BMP = New Bitmap(m_FMP.Width, m_FMP.Height)
            m_Rendered = True
            CreateBMP()
        End Set
    End Property

    Public ReadOnly Property MapBitmap() As Bitmap
        Get
            Return m_BMP
        End Get
    End Property

    Public Property MapOpened() As Boolean
        Get
            Return m_MapOpened
        End Get
        Set(ByVal Value As Boolean)
            m_MapOpened = Value
        End Set
    End Property

#End Region


    Public Sub DrawScaledMap(ByVal g As Graphics)
        Try
            g.DrawImage(m_BMP, g.VisibleClipBounds)
        Catch ex As Exception

        End Try
    End Sub


#Region "Drawing Support"

    Public Sub DrawLOSRays(ByRef g As Graphics, ByVal pnt As Point, ByVal LOSRange As Integer, ByVal offset As Point, Optional ByVal Power As Single = 1)
        '// draws LOS rays from the point up to the range... yeah
        Dim MoveStep As Single = 5

        Try
            Dim target As New PointF
            Dim sm As New SMVector
            Dim samplePoint As New PointF(pnt.X + LOSRange, pnt.Y)
            Dim offsetstep As Integer = 20
            Dim totalCost As Single

            '// a little ineffecient, but were testing
            Dim q1path As New GraphicsPath
            Dim q2path As New GraphicsPath
            Dim q3path As New GraphicsPath
            Dim q4path As New GraphicsPath

            Dim q1pointsies As New ArrayList(10)
            Dim q2pointsies As New ArrayList(10)
            Dim q3pointsies As New ArrayList(10)
            Dim q4pointsies As New ArrayList(10)

            Dim adjLOSRange As Single = LOSRange * FMPData.PixelsPerOneMile
            Dim LOSCost As Single
            Dim diff As Single

            Dim sampXoffset As Single = adjLOSRange
            Dim sampYoffset As Single = 0

            Dim done As Boolean
            Dim i As Integer

            'q1
            sampXoffset = adjLOSRange
            sampYoffset = 0
            q1pointsies.Add(New PointF(pnt.X, pnt.Y))

            Do Until sampXoffset < 0

                target.X = pnt.X + (sampXoffset)
                target.Y = pnt.Y + (sampYoffset)

                target = sm.GeneratePtOnVector(New PointF(pnt.X, pnt.Y), target, adjLOSRange / 2)

                'MyBase.DrawVertex(g, target, 6, Color.Red)

                done = False
                i = 0
                totalCost = 0
                Do Until done
                    i += MoveStep
                    samplePoint = sm.GeneratePtOnVector(New PointF(pnt.X, pnt.Y), target, i)

                    LOSCost = FMPData.LOSCost(New Point(samplePoint.X, samplePoint.Y))
                    If LOSCost > 1 Then
                        diff = LOSCost - 1
                        diff *= Power
                        LOSCost = diff + 1
                        If LOSCost < 1 Then LOSCost = 1
                    End If

                    totalCost += LOSCost * MoveStep

                    'MyBase.DrawVertex(g, samplePoint, 4, Color.Yellow)
                    If totalCost >= adjLOSRange / 2 Then
                        q1pointsies.Add(New PointF(samplePoint.X, samplePoint.Y))
                        done = True
                    End If
                Loop

                sampXoffset -= offsetstep
                sampYoffset += offsetstep
            Loop


            'q2
            sampXoffset = adjLOSRange
            sampYoffset = 0
            q2pointsies.Add(New PointF(pnt.X, pnt.Y))

            Do Until sampXoffset < 0

                target.X = pnt.X + (sampXoffset)
                target.Y = pnt.Y + (sampYoffset)

                target = sm.GeneratePtOnVector(New PointF(pnt.X, pnt.Y), target, adjLOSRange / 2)

                'MyBase.DrawVertex(g, target, 6, Color.Red)

                done = False
                i = 0
                totalCost = 0
                Do Until done
                    i += MoveStep
                    samplePoint = sm.GeneratePtOnVector(New PointF(pnt.X, pnt.Y), target, i)

                    LOSCost = FMPData.LOSCost(New Point(samplePoint.X, samplePoint.Y))
                    If LOSCost > 1 Then
                        diff = LOSCost - 1
                        diff *= Power
                        LOSCost = diff + 1
                        If LOSCost < 1 Then LOSCost = 1
                    End If

                    totalCost += LOSCost * MoveStep

                    'MyBase.DrawVertex(g, samplePoint, 4, Color.Yellow)
                    If totalCost >= adjLOSRange / 2 Then
                        q2pointsies.Add(New PointF(samplePoint.X, samplePoint.Y))
                        done = True
                    End If
                Loop

                sampXoffset -= offsetstep
                sampYoffset -= offsetstep
            Loop

            'q3
            sampXoffset = -1 * adjLOSRange
            sampYoffset = 0
            q3pointsies.Add(New PointF(pnt.X, pnt.Y))

            Do Until sampXoffset >= offsetstep

                target.X = pnt.X + (sampXoffset)
                target.Y = pnt.Y + (sampYoffset)

                target = sm.GeneratePtOnVector(New PointF(pnt.X, pnt.Y), target, adjLOSRange / 2)

                'MyBase.DrawVertex(g, target, 6, Color.Red)

                done = False
                i = 0
                totalCost = 0
                Do Until done
                    i += MoveStep
                    samplePoint = sm.GeneratePtOnVector(New PointF(pnt.X, pnt.Y), target, i)

                    LOSCost = FMPData.LOSCost(New Point(samplePoint.X, samplePoint.Y))
                    If LOSCost > 1 Then
                        diff = LOSCost - 1
                        diff *= Power
                        LOSCost = diff + 1
                        If LOSCost < 1 Then LOSCost = 1
                    End If

                    totalCost += LOSCost * MoveStep

                    'MyBase.DrawVertex(g, samplePoint, 4, Color.Yellow)
                    If totalCost >= adjLOSRange / 2 Then
                        q3pointsies.Add(New PointF(samplePoint.X, samplePoint.Y))
                        done = True
                    End If
                Loop

                sampXoffset += offsetstep
                sampYoffset += offsetstep
            Loop

            'q4()
            sampXoffset = -1 * adjLOSRange
            sampYoffset = 0
            q4pointsies.Add(New PointF(pnt.X, pnt.Y))

            Do Until sampXoffset >= offsetstep

                target.X = pnt.X + (sampXoffset)
                target.Y = pnt.Y + (sampYoffset)

                target = sm.GeneratePtOnVector(New PointF(pnt.X, pnt.Y), target, adjLOSRange / 2)

                'MyBase.DrawVertex(g, target, 6, Color.Red)

                done = False
                i = 0
                totalCost = 0
                Do Until done
                    i += MoveStep
                    samplePoint = sm.GeneratePtOnVector(New PointF(pnt.X, pnt.Y), target, i)

                    LOSCost = FMPData.LOSCost(New Point(samplePoint.X, samplePoint.Y))
                    If LOSCost > 1 Then
                        diff = LOSCost - 1
                        diff *= Power
                        LOSCost = diff + 1
                        If LOSCost < 1 Then LOSCost = 1
                    End If

                    totalCost += LOSCost * MoveStep

                    'MyBase.DrawVertex(g, samplePoint, 4, Color.Yellow)
                    If totalCost >= adjLOSRange / 2 Then
                        q4pointsies.Add(New PointF(samplePoint.X, samplePoint.Y))
                        done = True
                    End If
                Loop

                sampXoffset += offsetstep
                sampYoffset -= offsetstep
            Loop

            Dim obrush As New SolidBrush(Color.FromArgb(155, 255, 255, 255))

            ' now... draw the LOS ... path circle thingie
            '// offset the difference of draw location and map location

            For i = 0 To q1pointsies.Count - 1
                q1pointsies.Item(i) = New Point(q1pointsies.Item(i).x - offset.X, q1pointsies.Item(i).y - offset.Y)
            Next

            For i = 0 To q2pointsies.Count - 1
                q2pointsies.Item(i) = New Point(q2pointsies.Item(i).x - offset.X, q2pointsies.Item(i).y - offset.Y)
            Next

            For i = 0 To q3pointsies.Count - 1
                q3pointsies.Item(i) = New Point(q3pointsies.Item(i).x - offset.X, q3pointsies.Item(i).y - offset.Y)
            Next

            For i = 0 To q4pointsies.Count - 1
                q4pointsies.Item(i) = New Point(q4pointsies.Item(i).x - offset.X, q4pointsies.Item(i).y - offset.Y)
            Next


            '// now draw in the correct spot
            For i = 1 To q1pointsies.Count - 1
                q1path.AddLine(q1pointsies.Item(i - 1), q1pointsies.Item(i))
            Next
            g.FillPath(obrush, q1path)

            For i = 1 To q2pointsies.Count - 1
                q2path.AddLine(q2pointsies.Item(i - 1), q2pointsies.Item(i))
            Next
            g.FillPath(obrush, q2path)

            For i = 1 To q3pointsies.Count - 1
                q3path.AddLine(q3pointsies.Item(i - 1), q3pointsies.Item(i))
            Next
            g.FillPath(obrush, q3path)

            For i = 1 To q4pointsies.Count - 1
                q4path.AddLine(q4pointsies.Item(i - 1), q4pointsies.Item(i))
            Next
            g.FillPath(obrush, q4path)

            obrush.Dispose()


        Catch ex As Exception
            'HandleException("Campaigner Combat Engine.PointToPointDetection", ex)

        End Try

    End Sub

    Private Sub checkeredBorder(ByRef g As Graphics, ByVal pnt As Point, ByVal size As Integer)
        Try
            Dim fBorderPen1 As New Pen(Color.Yellow, 3)
            Dim fBorderPen2 As New Pen(Color.Black, 3)

            g.DrawRectangle(fBorderPen1, pnt.X - 1, pnt.Y - 1, CInt(size / 2), 1)
            g.DrawRectangle(fBorderPen2, pnt.X + CInt(size / 2) - 1, pnt.Y - 1, CInt(size / 2), 1)
            g.DrawRectangle(fBorderPen1, pnt.X + size - 1, pnt.Y - 1, 1, CInt(size / 2))
            g.DrawRectangle(fBorderPen2, pnt.X + size - 1, pnt.Y + CInt(size / 2) - 1, 1, CInt(size / 2))

            g.DrawRectangle(fBorderPen2, pnt.X - 1, pnt.Y - 1, 1, CInt(size / 2))
            g.DrawRectangle(fBorderPen1, pnt.X - 1, pnt.Y + CInt(size / 2) - 1, 1, CInt(size / 2))
            g.DrawRectangle(fBorderPen2, pnt.X - 1, pnt.Y + size - 1, CInt(size / 2), 1)
            g.DrawRectangle(fBorderPen1, pnt.X + CInt(size / 2) - 1, pnt.Y + size - 1, CInt(size / 2), 1)
        Catch ex As Exception

        End Try
    End Sub

    Private Sub drawReinforcementRange(ByRef g As Graphics, ByRef loc As Point, ByVal radius As Integer)

        Dim bl As New System.Drawing.Drawing2D.HatchBrush(Drawing2D.HatchStyle.SmallCheckerBoard, Color.DarkGreen, Color.Blue)

        g.SmoothingMode = SmoothingMode.HighSpeed

        Dim pa As New GraphicsPath()
        pa.AddEllipse(New Rectangle(loc.X - radius / 2 - 3, loc.Y - radius / 2 - 3, radius + 6, radius + 6))
        Dim cu As New GraphicsPath
        cu.AddEllipse(New Rectangle(loc.X - radius / 2, loc.Y - radius / 2, radius, radius))

        Dim reg As New Region
        reg.Intersect(pa)
        reg.Exclude(cu)

        g.FillRegion(bl, reg)
        g.DrawEllipse(Pens.Black, CInt(loc.X - radius / 2 - 3), CInt(loc.Y - radius / 2 - 3), radius + 6, radius + 6)
        g.DrawEllipse(Pens.LightBlue, (New Rectangle(loc.X - radius / 2, loc.Y - radius / 2, radius, radius)))

    End Sub

    Public Sub DrawZOC(ByVal gr As Graphics, ByVal pnt As Point, ByVal radius As Int16, Optional ByVal enemy As Boolean = False, Optional ByVal status As GroundUnitStatus_cgd = GroundUnitStatus_cgd.OK)

        Try
            If radius = 0 Then Exit Sub
            Dim c1(2) As Byte

            '// if the status is "funny" we will use a reduced radius as well.
            Select Case status
                Case GroundUnitStatus_cgd.Dead, GroundUnitStatus_cgd.Disarrayed, GroundUnitStatus_cgd.Assembling, GroundUnitStatus_cgd.NotDeployed, GroundUnitStatus_cgd.Scattered, GroundUnitStatus_cgd.Regrouping
                    '// no ZOC
                    Exit Sub
                Case GroundUnitStatus_cgd.OK, GroundUnitStatus_cgd.Regrouped
                    '// normal radius and green
                    c1(0) = 75
                    c1(1) = 255
                    c1(2) = 75
                Case Else
                    '// 1/2 radius and yellow
                    radius /= 2
                    c1(0) = 255
                    c1(1) = 255
                    c1(2) = 0
            End Select

            pnt.X -= radius / 2
            pnt.Y -= radius / 2

            If enemy Then
                gr.DrawEllipse(New Pen(Color.FromArgb(255, 255, 75, 75)), pnt.X, pnt.Y, radius, radius)
                gr.FillEllipse(New SolidBrush(Color.FromArgb(100, 255, 75, 75)), pnt.X, pnt.Y, radius, radius)

            Else
                gr.DrawEllipse(New Pen(Color.FromArgb(255, c1(0), c1(1), c1(2))), pnt.X, pnt.Y, radius, radius)
                gr.FillEllipse(New SolidBrush(Color.FromArgb(100, c1(0), c1(1), c1(2))), pnt.X, pnt.Y, radius, radius)
            End If
        Catch ex As Exception
            'HandleException(c_Module, ex)
        End Try

    End Sub


    Public Sub DrawFilledRectangle(ByVal gr As Graphics, ByVal x As Single, ByVal y As Single, ByVal width As Single, ByVal height As Single)
        gr.FillRectangle(Brushes.Blue, x, y, width, height)

    End Sub

    Public Sub DrawBorderedCircle(ByVal gr As Graphics, ByVal pnt As Point, ByVal radius As Int16, ByVal outterColor As Color, ByVal innerColor As Color)
        Dim outter As New Pen(outterColor)
        Dim inner As New Pen(innerColor)

        pnt.X -= radius / 2
        pnt.Y -= radius / 2

        gr.DrawEllipse(outter, pnt.X, pnt.Y, radius, radius)
        gr.DrawEllipse(inner, pnt.X + 1, pnt.Y + 1, radius - 2, radius - 2)
        gr.DrawEllipse(outter, pnt.X + 2, pnt.Y + 2, radius - 4, radius - 4)


    End Sub

#End Region


End Class



Public Enum IconSize
    NotShown = 0
    Small = 1
    Normal = 2
End Enum

Public Enum DisplayType_dt
    NotShown = 0
    Reduced = 1 '// this is also "selected only" option
    Normal = 2
End Enum


<Serializable()> Public Class CampaignerImageLibrary
    '// this holds all of the images used to create a wonderful, successful campaigner base map.
    Private _temporaryBridge As image
    Private _smallBridge As image
    Private _mediumBridge As image
    Private _heavyBridge As image

    Private _deadFriendly As image
    Private _deadEnemy As image

    Private _spottedEnemy As image
    Private _unknownIcon As image

    Private _battle As Image
    Private _lightningBolt As Image

    Private _inactiverule As Image


    Public Sub New()

    End Sub

    Public Property inactiveRule() As Image
        Get
            Return _inactiverule
        End Get
        Set(ByVal value As Image)
            _inactiverule = value
        End Set
    End Property

    Public Property spottedEnemy() As Image
        Get
            Return _spottedEnemy
        End Get
        Set(ByVal value As Image)
            _spottedEnemy = value
        End Set
    End Property

    Public Property unknownIcon() As Image
        Get
            Return _unknownIcon
        End Get
        Set(ByVal value As Image)
            _unknownIcon = value
        End Set
    End Property

    Public Property battleIcon() As Image
        Get
            Return _battle
        End Get
        Set(ByVal value As Image)
            _battle = value
        End Set
    End Property

    Public Property lightningBolt() As Image
        Get
            Return _lightningBolt
        End Get
        Set(ByVal value As Image)
            _lightningBolt = value
        End Set
    End Property

    Public Property smallBridge() As Image
        Get
            Return _smallBridge
        End Get
        Set(ByVal value As Image)
            _smallBridge = value
        End Set
    End Property

    Public Property mediumBridge() As Image
        Get
            Return _mediumBridge
        End Get
        Set(ByVal value As Image)
            _mediumBridge = value
        End Set
    End Property

    Public Property heavyBridge() As Image
        Get
            Return _heavyBridge
        End Get
        Set(ByVal value As Image)
            _heavyBridge = value
        End Set
    End Property

    Public Property deadFriendly() As Image
        Get
            Return _deadFriendly
        End Get
        Set(ByVal value As Image)
            _deadFriendly = value
        End Set
    End Property

    Public Property deadEnemy() As Image
        Get
            Return _deadEnemy
        End Get
        Set(ByVal value As Image)
            _deadEnemy = value
        End Set
    End Property

    Public Property temporaryBridge() As Image
        Get
            Return _temporaryBridge
        End Get
        Set(ByVal value As Image)
            _temporaryBridge = value
        End Set
    End Property

End Class


'Public Interface IWaypointLine
'    Property WaypointSize() As Single
'    Property WaypointColor() As Color
'    Property pnts() As ArrayList
'    Sub SetWaypointColor()
'    Sub Draw(ByVal g As Graphics)

'End Interface


'Public Class WaypointLine
'    Implements IWaypointLine

'    ' constants
'    Public Const WAYPOINTDEFAULTSIZE As Integer = 5

'    ' the properties of a waypoint line :)
'    Shared mPen As New Pen(Color.LightGreen)    ' so all waypoints are the same color dude.
'    Friend WithEvents cdlgPick As System.Windows.Forms.ColorDialog

'    ' member variables
'    Private mPoints As New ArrayList(6)

'    ' events
'    Public Event RefreshRequest()

'    Public Sub New()
'        mPen.StartCap = LineCap.RoundAnchor
'        mPen.EndCap = LineCap.ArrowAnchor
'        mPen.Width = WAYPOINTDEFAULTSIZE
'        'mPen.DashStyle = DashStyle.DashDot
'        Me.cdlgPick = New System.Windows.Forms.ColorDialog()
'    End Sub

'    Public Property WaypointSize() As Single Implements IWaypointLine.WaypointSize
'        Get
'            Return mPen.Width
'        End Get
'        Set(ByVal Value As Single)
'            mPen.Width = Value
'        End Set
'    End Property

'    Public Sub SetWaypointColor() Implements IWaypointLine.SetWaypointColor
'        Dim diagRes As New DialogResult()
'        diagRes = cdlgPick.ShowDialog()

'        If diagRes = DialogResult.Cancel Then Exit Sub
'        mPen.Color = cdlgPick.Color
'        RaiseEvent RefreshRequest()
'    End Sub

'    Public Property WaypointColor() As Color Implements IWaypointLine.WaypointColor
'        Get
'            Return mPen.Color
'        End Get
'        Set(ByVal Value As Color)
'            mPen.Color = Value
'        End Set
'    End Property

'    Public Overridable Sub Draw(ByVal g As Graphics) Implements IWaypointLine.Draw
'        Dim pth As New GraphicsPath()
'        Dim blackPen As New Pen(Color.Black)
'        Dim i As Int16
'        Dim thisPoint As Point
'        Dim LastPoint As Point

'        blackPen.StartCap = LineCap.NoAnchor
'        blackPen.EndCap = LineCap.ArrowAnchor
'        blackPen.Width = WAYPOINTDEFAULTSIZE + 2
'        blackPen.Color = Color.Black

'        For i = 1 To mPoints.Count - 1
'            thisPoint = mPoints(i)
'            LastPoint = mPoints(i - 1)
'            pth.AddLine(LastPoint, thisPoint)
'        Next

'        g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
'        g.DrawPath(blackPen, pth)
'        g.DrawPath(mPen, pth)

'    End Sub

'    Public Property pnts() As ArrayList Implements IWaypointLine.pnts
'        Get
'            Return mPoints
'        End Get
'        Set(ByVal Value As ArrayList)
'            mPoints = Value
'        End Set
'    End Property

'End Class


'Public Class SelectedUnitWaypointLine
'    Inherits WaypointLine
'    Private yellowPen As New Pen(Color.Yellow)

'    Public Sub New()
'        yellowPen.StartCap = LineCap.RoundAnchor
'        yellowPen.EndCap = LineCap.ArrowAnchor
'        yellowPen.Width = WAYPOINTDEFAULTSIZE
'    End Sub

'    Public Overrides Sub Draw(ByVal g As Graphics)
'        Dim pth As New GraphicsPath()
'        Dim blackPen As New Pen(Color.Black)
'        Dim i As Int16
'        Dim thisPoint As Point
'        Dim LastPoint As Point

'        blackPen.StartCap = LineCap.NoAnchor
'        blackPen.EndCap = LineCap.ArrowAnchor
'        blackPen.Width = WAYPOINTDEFAULTSIZE + 2
'        blackPen.Color = Color.Black

'        For i = 1 To MyBase.pnts.Count - 1
'            thisPoint = MyBase.pnts(i)
'            LastPoint = MyBase.pnts(i - 1)
'            pth.AddLine(LastPoint, thisPoint)
'        Next

'        g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
'        g.DrawPath(blackPen, pth)
'        g.DrawPath(yellowPen, pth)

'    End Sub

'End Class


'Public Class FriendlyPlayerWaypoint
'    Inherits WaypointLine
'    Private bluePen As New Pen(Color.LightGray)

'    Public Sub New()
'        bluePen.StartCap = LineCap.RoundAnchor
'        bluePen.EndCap = LineCap.ArrowAnchor
'        bluePen.Width = WAYPOINTDEFAULTSIZE
'    End Sub

'    Public Overrides Sub Draw(ByVal g As Graphics)
'        Dim pth As New GraphicsPath()
'        Dim blackPen As New Pen(Color.Black)
'        Dim i As Int16
'        Dim thisPoint As Point
'        Dim LastPoint As Point

'        blackPen.StartCap = LineCap.NoAnchor
'        blackPen.EndCap = LineCap.ArrowAnchor
'        blackPen.Width = WAYPOINTDEFAULTSIZE + 2
'        blackPen.Color = Color.Black

'        For i = 1 To MyBase.pnts.Count - 1
'            thisPoint = MyBase.pnts(i)
'            LastPoint = MyBase.pnts(i - 1)
'            pth.AddLine(LastPoint, thisPoint)
'        Next

'        g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
'        g.DrawPath(blackPen, pth)
'        g.DrawPath(bluePen, pth)

'    End Sub

'End Class


'Public Class InvalidWaypoint
'    Inherits WaypointLine
'    Private bluePen As New Pen(Color.Crimson)

'    Public Sub New()
'        bluePen.StartCap = LineCap.RoundAnchor
'        bluePen.EndCap = LineCap.ArrowAnchor
'        bluePen.Width = WAYPOINTDEFAULTSIZE
'    End Sub

'    Public Overrides Sub Draw(ByVal g As Graphics)
'        Dim pth As New GraphicsPath()
'        Dim i As Int16
'        Dim thisPoint As Point
'        Dim LastPoint As Point
'        Dim blackPen As New Pen(Color.Black)

'        blackPen.StartCap = LineCap.NoAnchor
'        blackPen.EndCap = LineCap.ArrowAnchor
'        blackPen.Width = WAYPOINTDEFAULTSIZE + 2
'        blackPen.Color = Color.Black

'        For i = 1 To MyBase.pnts.Count - 1
'            thisPoint = MyBase.pnts(i)
'            LastPoint = MyBase.pnts(i - 1)
'            pth.AddLine(LastPoint, thisPoint)
'        Next

'        g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
'        g.DrawPath(blackPen, pth)
'        g.DrawPath(bluePen, pth)
'    End Sub

'End Class
